<?php

namespace App\Api\Sms;

/**
 * https://docs.qq.com/doc/DRUlEamNJWFFhVmVR
 */

use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Support\Facades\DB;
use App\Api\SetWayAse;
use App\Console\Commands\Commission;

use function GuzzleHttp\json_decode;

class KaixinLianLian extends SMSChannel
{
  public $pname;
  public $log;
  public $root;
  public function __construct()
  {
    $this->pname = 'KaixinLianLian';
    $this->log = new Logger('KaixinLianLian');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/KaixinLianLian.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
    $this->root = new Logger('KaixinLianLianRootLog');
    $this->root->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/KaixinLianLianProRootLog.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  public function sendSms($data)
  {
    // var_dump($data);
    // die();
    if (empty($data['user_url'])) {
      return $this->resMsg(101, '第三方访问路径为空');
    }
    if (empty($data['user_name'])) {
      return $this->resMsg(102, '未查询到用户信息');
    }
    if (empty($data['user_pass'])) {
      return $this->resMsg(103, '第三方密匙为空');
    }
    if (empty($data['content'])) {
      return $this->resMsg(103, '发送的内容不能为空');
    }
    if (empty($data['phone'])) {
      return $this->resMsg(103, '发送的手机号不能为空');
    }
    if (is_array($data['phone'])) {
      $data['phone'] = implode(',', $data['phone']);
    }
    // if (empty($this->grant_type)) {
    //   return $this->resMsg(104, '第三方访问类型不能为空');
    // }
    $time = date('YmdHis');
    $form = [
      'name' => $data['user_name'],
      'seed' => $time,
      'key' => md5(md5($data['user_pass']) . $time),
      'dest' => $data['phone'],
      // 'content' => '【**商城】您的商品已发货，物流单号SF1305866598485。请确认快递员配送至您指定的地点后再提供签收密码，签收后请按照激活单页完成实名认证。客服热线4006410001',
      'content' => $data['content'],
    ];
    $result = $this->requestSms($data['user_url'], $form);
    $this->root->info('参数:' . json_encode($form) . '返回结果:' . json_encode($result));
    // if(isset($result['SMSReceiver']['ResultCode']) && $result['SMSReceiver']['ResultCode'] == 0){
    //   //数据库操作
    //   DB::table(Prefix.'wlksms')->insert(['msgid'=>$result['SMSReceiver']['SendSMSId'],'cardno'=> $data['phone']]);
    // }
    if (!empty($result['success'])) {
      return ['code' => 200, 'msg' => '发送成功', 'data' => $result];
    }
    return ['code' => -1, 'msg' => '发送失败'];
  }
  // 查询余额
  public function getBalance($data)
  {
    // return ['code' => -1, 'msg' => '发送失败'];
    $method = 'http://121.89.236.101:8080/eums/sms/utf8/balance.do';
    $time = date('YmdHis');
    $data = [
      'name' => $data['user_name'],
      'seed' => $time,
      'key' => md5(md5($data['user_pass']) . $time),
    ];
    // var_dump($method, 555555555);
    $result = $this->requestSms($method, $data);
    $this->root->info('参数:' . json_encode($data) . '返回结果:' . json_encode($result));
    if (!empty($result['success'])) {
      return ['code' => 200, 'msg' => '发送成功', 'data' => $result];
    }
    return ['code' => -1, 'msg' => '发送失败'];
  }
  // 查看回复信息
  //发送请求
  public function requestSms($url, $data)
  {
    // $url = $this->url.'?username= lizhe3204&password=lizhe7921&mobiles=&xmlmode=xmlmode&content=&targetdate=';
    // $form = [];
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    //传递 header 头
    // curl_setopt($curl, CURLOPT_HTTPHEADER, []);
    //对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    //从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    //post请求
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置提交的字符串
    // if ($isAccress) {
    //   curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($form)); //http_build_query(
    // } else {
    //   curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($form)); //http_build_query(
    // }
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); //http_build_query(

    //设置超时时间
    curl_setopt($curl, CURLOPT_TIMEOUT, 60000);
    $output = curl_exec($curl);
    // var_dump($output,1111111);
    if (curl_errno($curl)) {
      return ['status' => -1, 'msg' => curl_error($curl)];
      return false;
    }
    curl_close($curl);
    $this->root->info('发送返回参数' . $output);
    // $res = xml_to_data($output);
    return strToKeyValArr($output);
    // return json_decode($output, true);
  }
}
